package com.shop.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * com.shop.controller文档说明
 *
 * @author Angluzha
 * @version 2022/9/16
 */
@WebServlet("/image")
public class ImageController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp){
        try {
            String imageName = req.getParameter("imageName");
            FileInputStream stream = new FileInputStream("D:/Java/images/"+imageName);
            IOUtils.copy(stream,resp.getOutputStream());
            if (stream != null)
                stream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
